//
//  TTComposeView.swift
//  WeiBo
//
//  Created by 涂永江 on 2017/6/12.
//  Copyright © 2017年 涂永江. All rights reserved.
//

import UIKit
import pop
import YYModel

class TTComposeView: UIView {
    
    // MARK:  --懒加载视图
    
    private lazy var iamge:UIImageView = {
        
        let image1 = self.printscreen().applyLightEffect()
        let image  = UIImageView(image: image1)
        //图片要模糊
        
        return image
    }()
    
    var dateArray:[TTMode] = {
    let arr = [TTMode]()
       
        return arr
    }()
    
    var Vc:UIViewController?
    
    var arr:[UIButton]? = [UIButton]()
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setUi()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUi()
    {
       self.dateArray = loadPilst()!
        
        addSubview(iamge)
        
        
        let btnWidth:CGFloat = 80
        let btnHeight:CGFloat = 110
        let magin = (UIScreen.main.bounds.size.width - 3*btnWidth)/4
        for i in 0..<self.dateArray.count{
            
            let model = self.dateArray[i]
            //列
            let lie:CGFloat = CGFloat(i%3)
            //行
            let row:CGFloat = CGFloat(i/3)
            
            let btnX = lie*btnWidth + (lie + 1)*magin
            let btnY = row*btnHeight + row*magin + UIScreen.main.bounds.size.height
            
            self.frame.size = CGSize(width: screnssWidth, height: screnssHeight)
            let uibutton = TTSynchysisButton(frame: CGRect(x: btnX, y: btnY, width: btnWidth, height: btnHeight))
            //添加点击事件
            uibutton.addTarget(self, action: #selector(jumto(btn:)), for: .touchUpInside)
            uibutton.setTitle(model.title, for: .normal)
            uibutton.setImage(UIImage(named:model.icon!), for: .normal)
            uibutton.setTitleColor(UIColor.black, for: .normal)
            addSubview(uibutton)
            arr?.append(uibutton)
            
        }
        startPopAnimation(isUp: true)
        
        
    }
    
    //按钮的点击
    func jumto(btn:UIButton)
    {
        UIView.animate(withDuration: 0.25, animations: {
            
            for button in self.arr!{
                if button == btn{
                    
                    button.transform = CGAffineTransform(scaleX: 2, y: 2)
                    
                }else
                {
                    button.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
                    button.alpha = 0.3
                }
            }
            
        }) { (_) in
            UIView.animate(withDuration: 0.25, animations: {
                for button in self.arr!{
                    
                    button.transform = CGAffineTransform.identity
                    button.alpha = 1
                }
            }, completion: { (_) in
                //动画结束后跳转页面
                
                //通过字符串转换成类名然后创建,前面要加上命名空间
                let model = self.dateArray[btn.tag]
                
                let VcClass = NSClassFromString(model.className!) as! UIViewController.Type
                let tweetVc = VcClass.init()
                let nav = TTNavViewController(rootViewController: tweetVc)
                
                self.Vc?.present(nav, animated: true) {
                    self.removeFromSuperview()
                }
            })
            
        }
        
        
    }
    
    // MARK:  -- 截取window图片图片
    func printscreen() ->UIImage
    {
        let window = UIApplication.shared.keyWindow
        //开启图片上下文
        UIGraphicsBeginImageContext((window?.frame.size)!)
        //截取window的图片
        window?.drawHierarchy(in: CGRect(origin: CGPoint.zero, size: (window?.frame.size)!), afterScreenUpdates: false)
        
        let image = UIGraphicsGetImageFromCurrentImageContext()
        //关闭图片上下文
        UIGraphicsEndImageContext()
        return image!
    }
    //  开启pop动画
    private func startPopAnimation(isUp: Bool) {
        
        if isUp == false {
            //  向下pop动画
            arr = arr?.reversed()
        }
        
        for (i, button) in (arr?.enumerated())! {
            //  创建弹簧动画对象
            let popSpringAnimation = POPSpringAnimation(propertyNamed: kPOPViewCenter)
            if isUp {
                //  动画执行目的地
                popSpringAnimation?.toValue = CGPoint(x: button.center.x, y: button.center.y - 350)
            } else {
                //  动画执行目的地
                popSpringAnimation?.toValue = CGPoint(x: button.center.x, y: button.center.y + 350)
            }
            
            //  设置幅度
            popSpringAnimation?.springBounciness = 10
            //  设置弹簧初始化速度
            popSpringAnimation?.springSpeed = 10
            //  设置动画间隔时间
            popSpringAnimation?.beginTime = CACurrentMediaTime() + Double(i) * 0.025
            
            //  执行pop动画
            button.pop_add(popSpringAnimation, forKey: nil)
        }
        
    }
    
    // 添加视图在里面完成
    func show(Vc:UIViewController)
    {
        //        let window = UIApplication.shared.keyWindow
        //        window?.addSubview(self)
        self.Vc = Vc
        self.Vc?.view.addSubview(self)
        
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        startPopAnimation(isUp: false)
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+1) {
            self.removeFromSuperview()
        }
        
    }
    
    private func loadPilst()->[TTMode]?
    {
    
   let pathFeil = Bundle.main.path(forResource: "compose.plist", ofType: nil)
        let arr = NSArray.init(contentsOfFile: pathFeil!) as! [[String:Any]]
        
      let modelArry = NSArray.yy_modelArray(with: TTMode.self, json: arr) as! [TTMode]
        
        return modelArry
    }
    
}

    

